package com.songcha.sakura.mvvm.comic.search

import android.os.Build
import android.os.Bundle
import android.view.View.NOT_FOCUSABLE
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.songcha.library_common.util.StatusBarUtil
import com.songcha.sakura.R
import com.songcha.sakura.bean.ComicListBean
import com.songcha.sakura.databinding.*
import com.songcha.sakura.ui.adapter.ComicAdapter
import com.songcha.sakura.ui.decoration.HomeComicItemDecoration
import com.songcha.sakura.util.*
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.songcha.library_common.ui.view.HeaderBarView
import com.songcha.sakura.BuildConfig
import com.songcha.sakura.ui.base.BaseComicRefreshLoadMoreMvvmActivity


class ComicSearchActivity : BaseComicRefreshLoadMoreMvvmActivity<ComicSearchViewModel, ActivitySearchComicBinding>() {
    private var mKey=""
    private var mAdapter:ComicAdapter<ActivitySearchComicBinding>?=null


    override fun getLayoutId(): Int {
        return R.layout.activity_search_comic
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        intent.extras?.let {
            mKey=it.getString("key")?:""
        }

        if(mKey.isEmpty()) return

        databinding.hbvTitle.setTitle(mKey)


        /*databinding.srlSearch.setOnRefreshListener {
            viewModel.onRefreshing()
            viewModel.searchComic(mKey)
        }

        databinding.srlSearch.setOnLoadMoreListener {
            viewModel.onLoadingMore()
            viewModel.searchComic(mKey)
            //databinding.srlSearch
        }*/

        /*databinding.srlSearch.setOnLoadMoreListener {

        }*/

       /* if(viewModel.isInit){
            return
        }*/

        onLoading()

       // viewModel.searchComic(mKey)

        /*viewModel.rlst.observe(this){
            if(it!=null && !viewModel.isLoadingMore){
                initData()
            }
        }*/

       /* viewModel.rlst.observe(this){
           if(it!=null && !viewModel.isRefreshing && !viewModel.isLoadingMore && it.size>0){
               initData()
           }
       }*/

        viewModel.saveSearchHistory(mKey)

    }

    /*fun initData() {
        databinding.rcvSearch.apply {

            layoutManager= StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)

            if(itemDecorationCount==0)
                addItemDecoration(HomeComicItemDecoration())

            mAdapter=ComicAdapter(context, R.layout.item_comic,viewModel.rlst.value!!)
            adapter=mAdapter

            setComicAdapterListener(mAdapter!!)
        }
    }*/

    override fun onGetData() {
        viewModel.searchComic(mKey)
    }

    override fun onGetLoadMoreData() {
        viewModel.searchComic(mKey)
    }

    override fun initListData() {
        databinding.rcvSearch.apply {

            layoutManager= StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)

            if(itemDecorationCount==0)
                addItemDecoration(HomeComicItemDecoration())

            mAdapter=ComicAdapter(context, R.layout.item_comic,viewModel.rlst.value!!)
            adapter=mAdapter

            setComicAdapterListener(mAdapter!!)
        }
    }



    private fun setComicAdapterListener(adapter: ComicAdapter<ActivitySearchComicBinding>){

        adapter.setOnItemChildClickListener{ad,view,position->
            val comic=ad.data[position] as ComicListBean.ComicBean
            MyJumpUtil.toComicDetail(this,comic)
        }

    }

    override fun onRetry() {
        super.onRetry()
        viewModel.searchComic(mKey)
    }

    override fun getLoadViewTopMargin(): Int {
        return HeaderBarView.DEFAULT_HEIGHT + StatusBarUtil.getStatusBarHeight(this)
    }

    override fun getRefreshLayout(): SmartRefreshLayout {
        return databinding.srlSearch
    }

    override fun getListAdapter(): RecyclerView.Adapter<BaseViewHolder>? {
        if(mAdapter==null) return null
        return mAdapter as  RecyclerView.Adapter<BaseViewHolder>
    }

    override fun getLoadErrorTip(): String {
        return "没有找到番剧喵～"
    }

    override fun getLoadNoDataTip(): String {
        return "没有找到番剧喵～"
    }

    override fun getLoadNoNetworkTip(): String {
        return "请检查网络喵～"
    }

}